package com.xsq.view.easykeyboard.impl;

import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.widget.PopupWindow;

import com.xsq.utils.ToastUtil;
import com.xsq.utils.Utils;
import com.xsq.view.easykeyboard.action.KeyBoardForTextViewActionListener;

/**
 * 深度定制的金额输入键盘
 * 类名：
 * 说明：键盘原生接口实现类，可继承重写
 */

public class SystemOnKeyboardForTextViewActionListener implements KeyboardView.OnKeyboardActionListener {

    private StringBuffer content;
    private PopupWindow popupWindow;
    private KeyBoardForTextViewActionListener mActionListener;

    public void setPopupWindow(PopupWindow popupWindow) {
        this.popupWindow = popupWindow;
    }

    public void setKeyActionListener(KeyBoardForTextViewActionListener listener){
        this.mActionListener = listener;
    }

    @Override
    public void onPress(int primaryCode) {
       if (primaryCode == 152) {
            content.delete(0, content.length());
        }
    }

    @Override
    public void onRelease(int primaryCode) {
    }

    @Override
    public void onKey(int primaryCode, int[] keyCodes) {
        if(mActionListener == null) {
            return;
        }
        if (null == content) {
            content = new StringBuffer();
        }
        if (primaryCode == Keyboard.KEYCODE_DONE) {// 隐藏键盘
            if (null != popupWindow && popupWindow.isShowing()) {
                popupWindow.dismiss();
            }
            mActionListener.onComplete();
        } else if (primaryCode == Keyboard.KEYCODE_DELETE) {// 回退
            if(content.length() > 0) {
                content.deleteCharAt(content.length() - 1);
                mActionListener.onClear(content.toString());
            }
        } else if (primaryCode == 152) {
            content.delete(0,content.length());
            mActionListener.onClearAll();
        }else {
            content.append(Character.toString((char) primaryCode));
            if(Utils.isPrice(content.toString())) {
                if("0.00".equals(content.toString())) {
                    content.deleteCharAt(content.length() - 1);
                    mActionListener.onClear(content.toString());
                    ToastUtil.showShort("交易金额在0.01~10000之间");
                    return;
                }
                double price = Double.valueOf(content.toString());
                if(price > 10000) {
                    content.deleteCharAt(content.length() - 1);
                    mActionListener.onClear(content.toString());
                    ToastUtil.showShort("交易金额在0.01~10000之间");
                }else {
                    mActionListener.onTextChange(content.toString());
                }
            }else {
                content.deleteCharAt(content.length() - 1);
                mActionListener.onClear(content.toString());
                ToastUtil.showShort("请输入正确的金额");
            }
        }
    }

    @Override
    public void onText(CharSequence text) {
    }

    @Override
    public void swipeLeft() {
    }

    @Override
    public void swipeRight() {
    }

    @Override
    public void swipeDown() {
    }

    @Override
    public void swipeUp() {
    }
}
